home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / ease-3.5 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-16  |  7.8 KB  |  252 lines

  1. #ifdef FLUKE
  2. # ifndef LINT
  3.     static char RCSid[] = "@(#)FLUKE  $Header: /home/kreskin/u0/barnett/Src/ease/src/RCS/main.c,v 3.2 1991/05/16 10:45:25 barnett Exp $";
  4. # endif LINT
  5. #endif FLUKE
  6.  
  7. /*
  8.  *      main.c     -- Main procedure for Ease Translator.
  9.  *
  10.  *      author     -- James S. Schoner, Purdue University Computing Center
  11.  *                        West Lafayette, Indiana  47907
  12.  *
  13.  *      date       -- July 9, 1985
  14.  *
  15.  *    Copyright (c) 1985 by Purdue Research Foundation
  16.  *
  17.  *    All rights reserved.
  18.  *
  19.  * $Log: main.c,v $
  20.  * Revision 3.2  1991/05/16  10:45:25  barnett
  21.  * Better support for System V machines
  22.  * Support for machines with read only text segments
  23.  *
  24.  * Revision 3.1  1991/02/25  22:09:52  barnett
  25.  * Fixed some portability problems
  26.  *
  27.  * Revision 2.2  1990/05/07  11:14:02  jeff
  28.  * Add support for the "-q" flag which controls whether input lines
  29.  * are passed through as comments in the output stream.
  30.  *
  31.  * Version 2.1  90/01/30  15:37:16  jeff
  32.  * Filter input file through cpp before processing it.
  33.  * 
  34.  * Revision 2.0  88/06/15  14:42:41  root
  35.  * Baseline release for net posting. ADR.
  36.  * 
  37.  */
  38.  
  39.  
  40. #ifndef CPP    /* filename of preprocessor */
  41. #    define CPP    "/lib/cpp"
  42. #endif    CPP
  43.  
  44. #ifndef CPPARGS        /* valid arguments  of preprocessor */
  45. #ifdef sun        /* Sun's cpp has more options - I guess */
  46. #    define CPPARGS    "BCHpPRTDIUY"
  47. #else
  48. #    define CPPARGS    "CDEIPU"
  49. #endif sun
  50. #endif    CPP
  51.  
  52. #include "fixstrings.h"
  53. #include <stdio.h>
  54. #include <ctype.h>
  55. #ifdef SYSV
  56. #define index strchr
  57. #define rindex strrchr
  58. #endif
  59. #ifdef MALLOC_DEBUG
  60. extern int malloc_debug();
  61. extern int malloc_verify();
  62. #endif /* MALLOC_DEBUG */
  63.  
  64. extern FILE *DIAGf;            /* diagnostic file */
  65. char *infile = 0;            /* input file name */
  66. char *outfile = 0;            /* output file name */
  67. extern void InitError (), 
  68. #ifndef    DATA_RW
  69.         InitStrOps (),
  70.         InitParser (),
  71. #endif
  72.         InitSymbolTable (),
  73.         DefScan (),
  74.         FatalError (),
  75.             PreLoad ();
  76.  
  77. int EchoInputAsComments = 1;        /* should input lines be echoed
  78.                      * as comments?
  79.                      */
  80. int ErrorCount;                /* translation error count */
  81. void GetArgs ();            /* gets arguments to "et"  */
  82.  
  83. #ifdef YYDEBUG
  84. extern int yydebug;
  85. #else
  86. static int yydebug;
  87. #endif
  88.  
  89. /*
  90.  *    main () -- Main procedure for the Ease Translator et.  If no files are 
  91.  *                  given as arguments to et, stdin is translated and written to 
  92.  *               stdout.  If one file is given, it is translated and written 
  93.  *               to stdout.  If two files are given, the first is translated
  94.  *               and written to the second.  If the first filename is "-",
  95.  *               standard input is assumed.  A translation is performed on 
  96.  *               valid Ease input only, producing a regular sendmail 
  97.  *           configuration file. 
  98.  *
  99.  */
  100. void
  101. main (argc, argv)
  102. int argc;        /* argument count for "et"  */
  103. char *argv[];        /* argument vector for "et" */
  104. {
  105. #ifdef MALLOC_DEBUG
  106.         malloc_debug(1);
  107. #endif MALLOC_DEBUG
  108.     InitError ();            /* initialize error conditions */
  109. #ifndef    DATA_RW
  110.     InitParser ();
  111.     InitStrOps();
  112.     
  113. #endif
  114.     InitSymbolTable ();        /* initialize the symbol table */
  115.     PreLoad ();            /* preload special identifiers */
  116.     GetArgs (argc, argv);        /* set up argument files       */
  117.     (void) yyparse ();        /* perform translation           */
  118.     if (fflush (stdout) == EOF)
  119.         FatalError ("Cannot flush output stream/file", (char *) NULL);
  120.     DefScan ();                /* warn about undefined idents */
  121.     if (ErrorCount)
  122.         fprintf (DIAGf, "\n\n*** %d error(s) detected.\n", ErrorCount);
  123.     exit (ErrorCount);
  124. }
  125.  
  126.  
  127. /*
  128.  *    GetArgs () -- Processes arguments to the Ease translator "et".  The
  129.  *              arguments are files (margv) which may replace either/both
  130.  *              of the files standard input and standard output.  The 
  131.  *              following cases are possible:
  132.  *            
  133.  *              -- et f.e f.cf
  134.  *                Translate Ease file f.e and write result
  135.  *                to f.cf.
  136.  *
  137.  *              -- et f.e
  138.  *                Translate Ease file f.e and write result to
  139.  *                standard output.
  140.  *
  141.  *              -- et - f.cf
  142.  *                Translate standard input and write result to
  143.  *                f.cf.
  144.  *
  145.  *              -- et
  146.  *                Translate standard input and write result to
  147.  *                standard output.
  148.  *
  149.  *              et also accepts arguments. These include the /lib/cpp arguments
  150.  *              and the -d argument for debugging grammars.
  151.  *
  152.  *              Finally, a message indicating the volatility of the 
  153.  *              Ease output is written.
  154.  *
  155.  */
  156. void
  157. GetArgs (margc, margv)
  158. register int   margc;        /* argument count  */
  159. register char **margv;        /* argument vector */
  160. {
  161.      int    cppflags = 0;
  162.      int    otherflags = 0;
  163.      int arg;
  164.  
  165.      for (arg = 1; arg < margc; ++arg) {        /* scan arguments */
  166.      if (margv[arg][0] == '-') {        /* a flag?*/
  167.          if (isalpha(margv[arg][1])) {    /* yes - a flag */
  168.          if (index(CPPARGS,margv[arg][1])) ++cppflags;    /* one belonging to the CPP */
  169.          else if (margv[arg][1] == 'd' ) yydebug = 1;
  170.          else if (margv[arg][1] == 'q' ) EchoInputAsComments = 0;
  171.          else ++otherflags;
  172.          } else if (! margv[arg][1]) {     /* this argument is just a '-' */
  173.          if ( (arg - yydebug - cppflags - otherflags) == 1 )
  174.            infile = margv[arg];    
  175.          else if ( (arg - yydebug - cppflags - otherflags) == 2 )
  176.            outfile = margv[arg];
  177.          else
  178.            FatalError ("Usage: et [-%s] [infile [outfile]]", CPPARGS);
  179.          } else {
  180.          FatalError ("Usage: et [-%s] [infile [outfile]]", CPPARGS);
  181.          } /* end if a -argument */
  182.      } else {    /* a filename - i guess */
  183.          if ( (arg - yydebug - cppflags - otherflags) == 1 )
  184.            infile = margv[arg];    
  185.          else if ( (arg - yydebug - cppflags - otherflags) == 2 )
  186.            outfile = margv[arg];
  187.          else
  188.            FatalError ("Usage: et [-d] [-q] [-%s] [infile [outfile]]", CPPARGS);
  189.      } /* end if a filename argument */
  190.      }    /* done with parsing all of the arguments */
  191.      if (otherflags)
  192.        FatalError ("Usage: et [-%s] [infile [outfile]]", CPPARGS);
  193.      if (infile && strcmp(infile,"-") )
  194.        if (freopen (infile, "r", stdin) == NULL)
  195.      FatalError ("Cannot open input stream/file:", infile);
  196.      if (outfile && strcmp(outfile,"-") )
  197.        if (freopen (outfile, "w", stdout) == NULL)
  198.      FatalError ("Cannot open output stream/file:", outfile);
  199.      if (cppflags && cpp(margc,margv))
  200.      FatalError ("Cannot open preprocessor", CPP);
  201.  
  202.     printf ("###################################################\n");
  203.     printf ("##                                               ##\n");
  204.     printf ("##  WARNING: THIS FILE IS THE OUTPUT OF THE      ##\n");
  205.     printf ("##           `EASE' PRECOMPILER FOR SENDMAIL     ##\n");
  206.     printf ("##           CONFIGURATION FILES.                ##\n");
  207.     printf ("##                                               ##\n");
  208.     printf ("##           MAKE MODIFICATIONS TO THE SOURCE    ##\n");
  209.     printf ("##           FILE ONLY.  CHANGES MADE DIRECTLY   ##\n");
  210.     printf ("##           TO THIS FILE WILL DISAPPEAR THE     ##\n");
  211.     printf ("##           NEXT TIME THAT EASE IS RUN.         ##\n");
  212.     printf ("##                                               ##\n");
  213.     printf ("##           $Revision: 3.2 $                    ##\n");
  214.     printf ("##                                               ##\n");
  215.     printf ("###################################################\n");
  216. }
  217.  
  218. /* cpp preprocessor code
  219.  * copied from Schreiner and Friedman's book:
  220.  * Introduction to Compiler Construction with Unix
  221.  *
  222.  * Bruce Barnett
  223.  */
  224.  
  225. int cpp(argc,argv)
  226.      int argc;
  227.      char **argv;
  228. {
  229.     char **argp, *cmd;
  230.     extern FILE *yyin;    /* for lex input */
  231.     extern FILE *popen();
  232.     int i;
  233.  
  234.     for (i = 0, argp = argv; *++argp; )
  235.       if (**argp == '-' &&
  236.       index(CPPARGS, (*argp)[1]))
  237.     i+=strlen(*argp) + 1;
  238.     if ( ! (cmd = (char *) calloc(i + sizeof CPP, sizeof(char))))
  239.       return -1;    /* no room */
  240.     (void ) strcpy(cmd,CPP);
  241.     for (argp = argv; *++argp; )
  242.       if (**argp == '-' &&
  243.       index(CPPARGS, (*argp)[1]))
  244.     strcat(cmd, " "), strcat(cmd, *argp);
  245.     if (yyin = popen(cmd,"r"))
  246.       i = 0;    /* all's well */
  247.     else
  248.       i = -1;    /* no preprocessor */
  249.     cfree(cmd);
  250.     return i;
  251. }
  252.